using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        protected ServerSocket service;
        
        public Form1()
        {
            InitializeComponent();
        }

        public void atualizaHosts(Socket soc)
        {
            CheckForIllegalCrossThreadCalls = false;

            this.listBox1.Items.Clear();

            for (int i = 0; i < service.ActiveConnections; i++)
            {
                this.listBox1.Items.Add(service.RemoteAddress(i));
            }

        }

        public void atualizaMensagem(Socket soc)
        {
            CheckForIllegalCrossThreadCalls = false;

            textBox1.Text = service.ReceivedText;

        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (service != null )
            {
                service.SendText(textBox1.Text, service.HostEmissor);
            }
        }


        private void button1_Click_1(object sender, EventArgs e)
        {
            service = new ServerSocket(2252);
            if (service.Active())
            {
                service.OnConnect += new ServerSocket.ConnectionDelegate(this.atualizaHosts);
                //service.OnDisconnect += new ServerSocket.ConnectionDelegate(this.atualizaHosts);
                service.OnRead += new ServerSocket.ConnectionDelegate(this.atualizaMensagem);
            }
            else
            {
                Console.WriteLine("falhou");
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            if (service != null)
            {
                service.Deactive();
            }
        }
    }
}